﻿Imports DTO
Public Class NhaXuatBanDAO
    Public Shared Function DanhSach_nhaxb_DAO(ByVal strSQL As String) As List(Of NhaXuatBanDTO)
        Dim kq As New List(Of NhaXuatBanDTO)()
        Dim datatable As DataTable = ketnoi.QR_SQL(strSQL)
        Dim nxb As NhaXuatBanDTO
        Dim i = 0
        While (i < datatable.Rows.Count)
            nxb = New NhaXuatBanDTO
            nxb.MaNhaXuatBan = datatable.Rows(i)("MaNXB").ToString
            nxb.TenNhaXuatBan = datatable.Rows(i)("TenNXB").ToString
            nxb.DiaChi = datatable.Rows(i)("DiaChi").ToString
            nxb.SDT = datatable.Rows(i)("SDT").ToString
            kq.Add(nxb)
            i = i + 1
        End While
        Return kq
    End Function
    Public Shared Function Them_Loai_DAO(ByVal ncc As NhaXuatBanDTO) As String
        Dim sql = ""
        If ncc.MaNhaXuatBan = "" Then
            Return "Lỗi: Chưa Nhập Mã Nhà Xuất Bản"
        End If
        If ncc.TenNhaXuatBan = "" Then
            'sql = "Insert Into LoaiSach(MaLS, TenLS) values('" + loaisach.MaLoaiSach + "',NULL)"
            'Hoặc có thể làm theo dạng:
            sql = String.Format("Insert Into NhaXB(MaNXB,TenNXB,DiaChi,SDT) values('{0}',NULL,NULL,NULL)", ncc.MaNhaXuatBan)
        Else
            sql = String.Format("Insert Into NhaXB(MaNXB,TenNXB,DiaChi,SDT) values('{0}',N'{1}',N'{2}',N'{3}')", ncc.MaNhaXuatBan, ncc.TenNhaXuatBan, ncc.DiaChi, ncc.SDT)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Sua_Loai_DAO(ByVal ncc As NhaXuatBanDTO, ByVal MaCu As String) As String
        Dim sql = ""
        If ncc.MaNhaXuatBan = "" Then
            Return "Lỗi: Chưa Nhập Mã Nhà Xuất Bản"
        End If
        If ncc.TenNhaXuatBan = "" Then
            sql = String.Format("Update NhaXB set MaNXB='{0}', TenNXB=NULL, DiaChi=NULL, SDT=NULL where MaNCC='{1}'", ncc.MaNhaXuatBan, MaCu)
        Else
            sql = String.Format("Update NhaXB set MaNXB='{0}', TenNXB=N'{1}', DiaChi=N'{2}', SDT=N'{3}' where MaNXB='{4}'", ncc.MaNhaXuatBan, ncc.TenNhaXuatBan, ncc.DiaChi, ncc.SDT, MaCu)
        End If
        Return ketnoi.NonQR_SQL(sql)
    End Function
    Public Shared Function Xoa_Loai_DAO(ByVal ncc As NhaXuatBanDTO) As String
        Dim sql = "Delete From NhaXB where MaNXB='" + ncc.MaNhaXuatBan + "'"
        Return ketnoi.NonQR_SQL(sql)
    End Function
End Class
